//
//  FirstSearchViewController.m
//  WULIHH
//
//  Created by tang on 16/4/5.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "FirstSearchViewController.h"
#import "Header.h"


#import "FirstSearchView.h"
#import "TagView.h"
#import "HotSearchWithRequestTableViewCell.h"
#import "HotSearchView.h"




#import "WuLiHotSearchRequest.h"


#import "ShowDetalImageViewController.h"

@interface FirstSearchViewController ()<FirstSearchViewDelegate, UITableViewDelegate, UITableViewDataSource,TagViewDelegate>

{
    FirstSearchView *firstSearchView;
    UITableView *mainTableView;
    
    TagView *hotTagView;
    TagView *historyTap;
    HotSearchView *hotSsarchView;
    
    
    
    UIView *headView;
    NSDictionary *infoDic;
    NSArray *workArray;
    NSArray *hotStarArray;
    NSArray *historyArray;
    
    UILabel *hotLable;
    UILabel *workLable;
    
}
@end

@implementation FirstSearchViewController


- (void)loadView
{
    [super loadView];
    
    
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationController setNavigationBarHidden:YES];
    
    firstSearchView = [[FirstSearchView alloc] initWithFrame:CGRectMake(0, 0, WithView, 64.0)];
    [self.view addSubview:firstSearchView];
    
    mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64.0, WithView, heightView-64.0)];
    [self.view addSubview:mainTableView];
    
    headView = [[UIView alloc] init];
    [self.view addSubview:headView];
    [headView setBackgroundColor:uicolor(233, 233, 233)];

    
    hotSsarchView = [[HotSearchView alloc] initWithFrame:CGRectMake(0, 64.0, WithView, heightView-64.0)];
    [self.view addSubview:hotSsarchView];
    [hotSsarchView setHidden:YES];
    
//热门标签
    hotTagView = [[TagView alloc] init];
    [headView addSubview:hotTagView];
    
//历史记录
    historyTap = [[TagView alloc] init];
    [headView addSubview:historyTap];
    
    hotLable = [[UILabel alloc] initWithFrame:CGRectZero];
    [hotLable setText:@"热门搜索"];
    [hotLable setTextColor:uicolor(104, 104, 104)];
    [hotLable setFont:[UIFont systemFontOfSize:11.0]];
    [headView addSubview:hotLable];

    workLable = [[UILabel alloc] initWithFrame:CGRectZero];
    [workLable setText:@"热门作品"];
    [workLable setTextColor:uicolor(104, 104, 104)];
    [workLable setFont:[UIFont systemFontOfSize:11.0]];
    [headView addSubview:workLable];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [hotTagView setMTagViewDelegate:self];
    [historyTap setMTagViewDelegate:self];
    
    [firstSearchView setMFirstSearchViewDelegate:self];
    
    [mainTableView setDataSource:self];
    [mainTableView setDelegate:self];
    [mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [hotTagView setTagViewButtonFont:11.0];
    [historyTap setTagViewButtonFont:11.0];

    infoDic = [[NSUserDefaults standardUserDefaults] valueForKey:qingqiuremenziyuan];
    if ([[infoDic valueForKey:@"success"] intValue] == 1)
    {
        

        workArray = [[infoDic valueForKey:@"data"] valueForKey:@"works"];
        hotStarArray = [[infoDic valueForKey:@"data"] valueForKey:@"hotKeywords"];
//        NSLog(@"%@", hotStarArray);
        if (hotStarArray.count == 0)
        {
            
        }
        else
        {
            [headView setFrame:CGRectMake(0, 0, WithView, 180.0)];
            [hotLable setFrame:CGRectMake(10, 13, 100.0, 12.0)];
            [hotTagView setFrame:CGRectMake(0, CGRectGetMaxY(hotLable.frame), WithView, 65)];
            hotTagView.haveSelected = [NSMutableArray arrayWithArray:hotStarArray];
            [workLable setFrame:CGRectMake(10, headView.bounds.size.height-20.0, headView.bounds.size.width-21.0, 10.0)];
            [mainTableView setTableHeaderView:headView];
        }
    }
    else
    {
        
    }
    
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WithView, heightView)];
    [view2 setBackgroundColor:[UIColor redColor]];
    [self.view.window addSubview:view2];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChangeSearcg:) name:wenzisousuo object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark-------datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [workArray count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInden = @"cell";
    HotSearchWithRequestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInden];
    if (!cell) {
        cell = [[HotSearchWithRequestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInden];
    }
    [cell setmodelWithInfo:workArray[indexPath.row]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"%@", workArray[indexPath.row]);
    ShowDetalImageViewController *detalView = [[ShowDetalImageViewController alloc] init];
    detalView.IDString = [NSString stringWithFormat:@"%@", [[workArray objectAtIndex:indexPath.row] valueForKey:@"id"]];
    detalView.textStr = [[workArray objectAtIndex:indexPath.row]valueForKey:@"title"];
    [self.navigationController pushViewController:detalView animated:YES];

}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [firstSearchView removeKeyBoard];
}

#pragma mark----获取气泡文字
- (void)getTagText:(NSString *)stirng
{
    [firstSearchView setFieldText:[NSString stringWithFormat:@"%@", stirng]];

//    NSLog(@"%@", stirng);
    if ([stirng length] == 0)
    {
        [hotSsarchView setHidden:YES];
    }
    else
    {
        [WuLiHotSearchRequest searchWithText:stirng];
    }
}



//改变输入文字
- (void)changeText:(NSString *)text
{
//    NSLog(@"%@", text);
    if ([text length] == 0)
    {
        [hotSsarchView setHidden:YES];
    }
    else
    {
        
    }
}


- (void)textChangeSearcg:(NSNotification *)notion
{
    NSDictionary *dic = [notion userInfo];
    NSLog(@"%@", dic);
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
//        NSLog(@"%@", dic);
        [hotSsarchView setInfoDic:dic];
        [hotSsarchView setHidden:NO];
    }
    else
    {
        
    }
}



/*取消搜索*/
- (void)disMissSearch
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
